define(['jquery', 'bootstrap', 'backend', 'echarts'], function ($, undefined, Backend, echarts) {
    
    var Controller = {
        index: function () {
            // 初始化看板
            this.initDashboard();
            
            // 定时刷新数据
            setInterval(function() {
                Controller.refreshData();
            }, 30000); // 30秒刷新一次
        },
        
        initDashboard: function() {
            // 初始化生产趋势图表
            this.initProductionChart();
            
            // 初始化设备状态图表
            this.initEquipmentChart();
            
            // 加载实时数据
            this.loadRealTimeData();
        },
        
        initProductionChart: function() {
            var productionChart = echarts.init(document.getElementById('production-chart'));
            
            var option = {
                title: {
                    text: '生产趋势',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['计划数量', '完成数量'],
                    bottom: 10
                },
                xAxis: {
                    type: 'category',
                    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: '计划数量',
                        type: 'line',
                        data: [120, 132, 101, 134, 90, 230, 210]
                    },
                    {
                        name: '完成数量',
                        type: 'line',
                        data: [110, 125, 95, 130, 85, 220, 200]
                    }
                ]
            };
            
            productionChart.setOption(option);
        },
        
        initEquipmentChart: function() {
            var equipmentChart = echarts.init(document.getElementById('equipment-chart'));
            
            var option = {
                title: {
                    text: '设备状态',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [
                    {
                        name: '设备状态',
                        type: 'pie',
                        radius: '50%',
                        data: [
                            {value: 15, name: '正常'},
                            {value: 3, name: '维修中'},
                            {value: 1, name: '故障'},
                            {value: 1, name: '报废'}
                        ],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            
            equipmentChart.setOption(option);
        },
        
        loadRealTimeData: function() {
            // 加载订单进度数据
            $.ajax({
                url: 'manufacture/dashboard/getOrderProgressData',
                type: 'GET',
                success: function(data) {
                    if (data.code === 1) {
                        Controller.updateOrderProgress(data.data);
                    }
                }
            });
            
            // 加载库存预警数据
            $.ajax({
                url: 'manufacture/dashboard/getStockWarningData',
                type: 'GET',
                success: function(data) {
                    if (data.code === 1) {
                        Controller.updateStockWarning(data.data);
                    }
                }
            });
            
            // 加载工作记录数据
            $.ajax({
                url: 'manufacture/dashboard/getWorkRecordsData',
                type: 'GET',
                success: function(data) {
                    if (data.code === 1) {
                        Controller.updateWorkRecords(data.data);
                    }
                }
            });
        },
        
        updateOrderProgress: function(data) {
            var html = '';
            data.forEach(function(item) {
                html += '<tr>';
                html += '<td>' + item.order_no + '</td>';
                html += '<td>' + item.customer_name + '</td>';
                html += '<td><div class="progress"><div class="progress-bar" style="width: ' + item.progress + '%">' + item.progress + '%</div></div></td>';
                html += '<td>' + item.status_text + '</td>';
                html += '</tr>';
            });
            $('#order-progress-table tbody').html(html);
        },
        
        updateStockWarning: function(data) {
            var html = '';
            data.forEach(function(item) {
                html += '<tr>';
                html += '<td>' + item.material_name + '</td>';
                html += '<td>' + item.warehouse_name + '</td>';
                html += '<td>' + item.quantity + '</td>';
                html += '<td>' + item.warning_quantity + '</td>';
                html += '</tr>';
            });
            $('#stock-warning-table tbody').html(html);
        },
        
        updateWorkRecords: function(data) {
            var html = '';
            data.forEach(function(item) {
                html += '<tr>';
                html += '<td>' + item.worker_name + '</td>';
                html += '<td>' + item.process_name + '</td>';
                html += '<td>' + item.quantity + '</td>';
                html += '<td>' + item.work_time + '</td>';
                html += '</tr>';
            });
            $('#work-records-table tbody').html(html);
        },
        
        refreshData: function() {
            this.loadRealTimeData();
        }
    };
    
    return Controller;
});
